package com.jeethink.erp.stock.service.impl;

import java.util.Date;
import java.util.List;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.erp.buy.domain.TbBuy;
import com.jeethink.erp.buy.mapper.TbBuyMapper;
import com.jeethink.erp.pile.domain.TbStockPile;
import com.jeethink.erp.pile.mapper.TbStockPileMapper;
import com.jeethink.erp.pileDetail.domain.TbStockPileDetail;
import com.jeethink.erp.pileDetail.mapper.TbStockPileDetailMapper;
import com.jeethink.erp.stockDetail.domain.TbEnterStockDetail;
import com.jeethink.erp.stockDetail.mapper.TbEnterStockDetailMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeethink.erp.stock.mapper.TbEnterStockMapper;
import com.jeethink.erp.stock.domain.TbEnterStock;
import com.jeethink.erp.stock.service.ITbEnterStockService;

/**
 * 入库单Service业务层处理
 * 
 * @author ypr
 * @date 2021-06-01
 */
@Service
public class TbEnterStockServiceImpl implements ITbEnterStockService 
{
    //入库单表
    @Autowired
    private TbEnterStockMapper tbEnterStockMapper;

    //入库单详细表
    @Autowired
    private TbEnterStockDetailMapper tbEnterStockDetailMapper;

    //进货表
    @Autowired
    private TbBuyMapper buyMapper;

    //库存表
    @Autowired
    private TbStockPileMapper tbStockPileMapper;

    @Autowired
    private TbStockPileDetailMapper tbStockPileDetailMapper;
    /**
     * 查询入库单
     * 
     * @param enterStockId 入库单ID
     * @return 入库单
     */
    @Override
    public TbEnterStock selectTbEnterStockById(Long enterStockId)
    {
        return tbEnterStockMapper.selectTbEnterStockById(enterStockId);
    }

    /**
     * 查询入库单列表
     * 
     * @param tbEnterStock 入库单
     * @return 入库单
     */
    @Override
    public List<TbEnterStock> selectTbEnterStockList(TbEnterStock tbEnterStock)
    {
        return tbEnterStockMapper.selectTbEnterStockList(tbEnterStock);
    }

    /**
     * 新增入库单
     * 
     * @param tbEnterStock 入库单 保存至主库
     * @return 结果
     */
    @Override
    public int insertTbEnterStock(TbEnterStock tbEnterStock)
    {

        //新增入库单
        tbEnterStock.setCreateTime(DateUtils.getNowDate());
        tbEnterStockMapper.insertTbEnterStock(tbEnterStock);
        //新增入库单详细信息
        TbEnterStockDetail stockDetail = new TbEnterStockDetail();
        stockDetail.setCommodityId(tbEnterStock.getCommodityId());
        stockDetail.setEnterStockId(tbEnterStock.getEnterStockId());
        stockDetail.setPrice(tbEnterStock.getPurchasePrice());
        stockDetail.setQuantity(Long.valueOf(tbEnterStock.getPurchaseNum()));
        tbEnterStockDetailMapper.insertTbEnterStockDetail(stockDetail);
        //修改进货表入库状态
        TbBuy buy = new TbBuy();
        buy.setBuyId(tbEnterStock.getBuyId());
        buy.setWarehouseYesNo("1");
        buyMapper.updateTbBuy(buy);
        //根据商品id,库存id 查询主库存表 如果有此商品 进行增加库存  如果没有 则新增
        TbStockPile pile = new TbStockPile();
        pile.setCommodityId(tbEnterStock.getCommodityId());
        pile.setShopId(tbEnterStock.getShopId());
        TbStockPile stockPile = tbStockPileMapper.selectCommodityId(pile);
        int result = 0;
        if(stockPile != null){
            int num = stockPile.getPurchaseNum();
            int nu = tbEnterStock.getPurchaseNum();
            int PurchaseNum = num+nu;
            stockPile.setPurchaseNum(PurchaseNum);
            tbStockPileMapper.updateTbStockPile(stockPile);
            TbStockPileDetail stockPileDetail = new TbStockPileDetail();
            stockPileDetail.setStockPileId(stockPile.getStockPileId());
            stockPileDetail.setInventoryOperation("0");
            stockPileDetail.setOperationNum(Long.valueOf(tbEnterStock.getPurchaseNum()));
            stockPileDetail.setOperationTime(new Date());
            result = tbStockPileDetailMapper.insertTbStockPileDetail(stockPileDetail);
        }else{
            TbStockPile tbStockPile = new TbStockPile();
            tbStockPile.setWarehouseId(tbEnterStock.getWarehouseId());
            tbStockPile.setCommodityId(tbEnterStock.getCommodityId());
            tbStockPile.setFirstEnterDate(new Date());
//            tbStockPile.setPurchasePrice(tbEnterStock.getPurchasePrice());
            tbStockPile.setShopId(tbEnterStock.getShopId());
            tbStockPile.setPurchaseNum(tbEnterStock.getPurchaseNum());
            //是否为主库存 0 是 1 否
            tbStockPile.setIsMainLibrary("0");
            tbStockPileMapper.insertTbStockPile(tbStockPile);

            TbStockPileDetail stockPileDetail = new TbStockPileDetail();
            stockPileDetail.setStockPileId(tbStockPile.getStockPileId());
            stockPileDetail.setInventoryOperation("0");
            stockPileDetail.setOperationNum(Long.valueOf(tbEnterStock.getPurchaseNum()));
            stockPileDetail.setOperationTime(new Date());
            result = tbStockPileDetailMapper.insertTbStockPileDetail(stockPileDetail);
        }


        return result;
    }

    /**
     * 修改入库单
     * 
     * @param tbEnterStock 入库单
     * @return 结果
     */
    @Override
    public int updateTbEnterStock(TbEnterStock tbEnterStock)
    {
        tbEnterStock.setUpdateTime(DateUtils.getNowDate());
        return tbEnterStockMapper.updateTbEnterStock(tbEnterStock);
    }

    /**
     * 批量删除入库单
     * 
     * @param enterStockIds 需要删除的入库单ID
     * @return 结果
     */
    @Override
    public int deleteTbEnterStockByIds(Long[] enterStockIds)
    {
        return tbEnterStockMapper.deleteTbEnterStockByIds(enterStockIds);
    }

    /**
     * 删除入库单信息
     * 
     * @param enterStockId 入库单ID
     * @return 结果
     */
    @Override
    public int deleteTbEnterStockById(Long enterStockId)
    {
        return tbEnterStockMapper.deleteTbEnterStockById(enterStockId);
    }
}
